package com.lhd.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.lhd.common.GmallConfig;
import redis.clients.jedis.Jedis;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class DimUtil {



/*
1.存redis   String
KEY:   DIM:表名:key  value
*
* */
    public static JSONObject getDimInfo(Connection connection, String tableName, String key) throws InvocationTargetException, SQLException, InstantiationException, IllegalAccessException {

        //先查询Redis
        Jedis jedis = JedisUtil.getJedis(); //创建jdbc链接、
        /*key 和 value*/

        String redisKey = "DIM:" + tableName + ":" + key;

        String dimJsonStr = jedis.get(redisKey);

        if (dimJsonStr != null) {
            //重置过期时间
            jedis.expire(redisKey, 24 * 60 * 60);
            //归还连接
            jedis.close();
            //返回维度数据
            return JSON.parseObject(dimJsonStr);
        }

        //拼接SQL语句
        String querySql = "select * from " + GmallConfig.HBASE_SCHEMA + "." + tableName + " where id='" + key + "'";
//        System.out.println("querySql>>>" + querySql);
        //查询数据
        List<JSONObject> queryList = JdbcUtil.queryList(connection, querySql, JSONObject.class, false);

//        System.out.println("queryList"+queryList+"----querySql:"+querySql);
//        //将从Phoenix查询到的数据写入Redis
        JSONObject dimInfo = queryList.get(0);
        jedis.set(redisKey, dimInfo.toJSONString());
//        //设置过期时间
        jedis.expire(redisKey, 24 * 60 * 60);
//        //归还连接
        jedis.close();

        //返回结果
        return dimInfo;
    }

    public static void delDimInfo(String tableName, String key) {
        //获取连接
        Jedis jedis = JedisUtil.getJedis();
        //删除数据
        jedis.del("DIM:" + tableName + ":" + key);
        //归还连接
        jedis.close();
    }


//    public static void main(String[] args) throws Exception {
//        //1.获取连接池
//        DruidDataSource dataSource = DruidDSUtil.createDataSource();  //耗时
//        //2.获取连接
//        DruidPooledConnection connection = dataSource.getConnection();
//
//        //3.调用方法获取维度数据
//        long start = System.currentTimeMillis();
//        JSONObject jsonObject = DimUtil.getDimInfo(connection, "DIM_SKU_INFO", "1");
//        long end = System.currentTimeMillis();
//        //239*6    1300   305  232   233    skuId   spuId    tm_id c1 c2  c3    2
//        //一开始我有关联没有  只关联一个skuId    预聚合操作 数据量   700
//        JSONObject jsonObject2 = DimUtil.getDimInfo(connection, "DIM_SKU_INFO", "1");
//        //12*6    72    每秒   100条
//        long end2 = System.currentTimeMillis();
//
//        System.out.println(end - start);
//        System.out.println(end2 - end);
//
//
//        System.out.println(jsonObject);
//        System.out.println(jsonObject2);
//    }
    public static void main(String[] args) throws Exception {

        DruidDataSource dataSource = DruidDSUtil.createDataSource();

        DruidPooledConnection connection = dataSource.getConnection();

        JSONObject dimInfo = getDimInfo(connection, "DIM_BASE_PROVINCE", "10");

        System.out.println(dimInfo);

        connection.close();

    }

}
